package com.maxd.service.article;

import com.maxd.model.Article;
import com.maxd.respository.article.ArticleJpaRepository;
import com.maxd.respository.article.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 文章
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleJpaRepository articleJpaRepository;
    @Autowired
    private ArticleRepository articleRepository;

    @Override
    public Article save(Article article) {
        return articleJpaRepository.save(article);
    }

    @Override
    public Article selectArticleById(Long articleId) {
        return articleRepository.selectArticleById(articleId);
    }

    @Override
    public Page<Article> selectArticleList(Long classifyId, Integer state, Pageable pageable) {
        return articleRepository.selectArticleList(classifyId,state,pageable);
    }

    @Override
    public void deleteArticleById(Long articleId) {
        articleJpaRepository.deleteById(articleId);
    }

    @Override
    @Transactional
    public void deleteArticleByIds(String articleIds) {
        for(String str:articleIds.split(",")){
            articleJpaRepository.deleteById(Long.parseLong(str));
        }

    }

    @Override
    public int updateArticleStateById(Long articleId, Integer state) {
        return articleRepository.updateArticleStateById(articleId,state);
    }

    @Override
    @Transactional
    public void updateArticleStateByIds(String articleIds) {
        for(String str:articleIds.split(",")){
            articleRepository.updateArticleStateById(Long.parseLong(str),2);
        }
    }
}
